//
//  BaseTableViewController.m
//  XNet
//
//  Created by easystudio on 6/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "BaseTableViewController.h"
#import "TaskManager.h"


@implementation BaseTableViewController
@synthesize _refreshHeaderView;
@synthesize _refreshFooterView;
#pragma mark - Defined by DHC

-(void) pushViewController:(UIViewController*) viewController;
{
//    if(_oNavigationRoot != nil)
//        [_oNavigationRoot nextView:viewController];
//    else
        [self.navigationController pushViewController:viewController animated:YES];
}
#pragma mark - Defined by DHC end



- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)dealloc
{
    [super dealloc];
    //[_oNavigationRoot release];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

        if (_refreshFooterView == nil) {
            
            EGORefreshTableFooterView *view = [[EGORefreshTableFooterView alloc] initWithFrame: CGRectMake(0.0f, self.tableView.contentSize.height, self.tableView.bounds.size.width,800)];
            //NSLog(@"%@", NSStringFromCGRect( view.frame ));
            view.delegate = self;
            [self.tableView addSubview:view];
            self._refreshFooterView = view;
            [view release];
            
        }
        if (_refreshHeaderView == nil) {
            
            EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame: CGRectMake(0,                                                                                                       -800,                                                                                                       self.tableView.bounds.size.width,                                                                                                       800)];
            //NSLog(@"%@", NSStringFromCGRect( view.frame ));
            view.delegate = self;
            [self.tableView addSubview:view];
           self._refreshHeaderView = view;
           [view release];
        }
	
	//  update the last update date
	[_refreshFooterView refreshLastUpdatedDate];
    [_refreshHeaderView refreshLastUpdatedDate];
    
    self.tableView.backgroundColor=BACKGROUND_COLOR;
    [self.view setBackgroundColor:BACKGROUND_COLOR];
   
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self._refreshFooterView = nil;
    self._refreshHeaderView = nil;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
}

#pragma mark -
#pragma mark Data Source Loading / Reloading Methods

//- (void)reloadTableViewDataSource{
//	
//	//  should be calling your tableviews data source model to reload
//	//  put here just for demo
//	_bIsLoading = YES;
//    NSLog(@"reload table view data source");
//    
//    //sleep(5);
//	
//}
//
//- (void)doneLoadingTableViewData{
//	
//	//  model should call this when its done loading
//	_bIsLoading = NO;
//    NSLog(@"done loading table view data");
//	[_refreshFooterView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
//	
//}

#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{	

	[_refreshFooterView egoRefreshScrollViewDidScroll:scrollView];
    [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

	[_refreshFooterView egoRefreshScrollViewDidEndDragging:scrollView];
    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
	
}
#pragma mark -
#pragma mark Subclass Should Recover these Methods To Get Data
-(void) getMoreData
{
    
    //[[[TaskManager Share] GetTaskByType:HOT_TOPIC_LIST Delegate:self] Excute];
    
}

-(void) refreshData
{
    //[[[TaskManager Share] GetTaskByType:HOT_TOPIC_LIST Delegate:self] Excute];
}

#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableFooterDidTriggerRefresh:(EGORefreshTableFooterView*)view{
	
    [self getMoreData];
    //NSLog(@"get more data");
	//[self reloadTableViewDataSource];
	//[self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:4.0];
	
}
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
	
    [self refreshData];
    //NSLog(@"get more data");
	//[self reloadTableViewDataSource];
	//[self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:4.0];
	
}

- (BOOL)egoRefreshTableFooterDataSourceIsLoading:(EGORefreshTableFooterView*)view{
	
	return _bIsLoading; // should return if data source model is reloading
	
}
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
	
	return _bIsLoading; // should return if data source model is reloading
	
}

- (NSDate*)egoRefreshTableFooterDataSourceLastUpdated:(EGORefreshTableFooterView*)view{
	
	return [NSDate date]; // should return date data source was last changed
	
}
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
	
	return [NSDate date]; // should return date data source was last changed
	
}
- (void)reDrawHeaderAndFooter
{
    if (_refreshFooterView != nil) {
        [_refreshFooterView setFrame:CGRectMake(0.0f, self.tableView.contentSize.height, self.tableView.bounds.size.width,800)];
    }
    if (_refreshHeaderView != nil) {
  
        [_refreshHeaderView setFrame:CGRectMake(0,                                                                                                       -800,                                                                                                       self.tableView.bounds.size.width,                                                                                                       800)];
        
    }

}
#pragma mark -
#pragma mark UserTaskDelegate Methods
- (void) OnPreExcute 
{
    _bIsLoading = YES;
    [_refreshHeaderView setState:EGOOPullRefreshLoading];
    [_refreshFooterView setState:EGOOPullRefreshLoading];
    //NSLog(@"do pre excute");
    //_bTaskRunning = YES;
}

- (void) DoInBackground
{
    //sleep(5);
    //[self getTopicDataByType];
}

- (void) OnPostExcute
{
    _bIsLoading = NO;
    [_refreshFooterView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
    [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];

    [self.tableView reloadData];
    [self reDrawHeaderAndFooter];
    //NSLog(@"do post excute");
}


@end
